package com.tanguy.user.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

/**
 * 微信支付回调Activity
 *
 * @author allen@tronsis.com
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        api = WXAPIFactory.createWXAPI(this, Const.INSTANCE.getWECHAT_APP_ID());
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    /**
     * 支付结果回调
     *
     * @param resp
     */
    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {
                case 0: // 成功
                    Toast.makeText(getApplication(), "支付成功", Toast.LENGTH_LONG).show();
//                    RxBus.get().post(Const.Action.INSTANCE.getPAY_SUCCESS_ACTION(),"");
                    break;
                case -1: // 错误
                    Toast.makeText(getApplication(), "支付失败", Toast.LENGTH_LONG)
                            .show();
                    break;
                case -2: // 用户取消支付
                    Toast.makeText(getApplication(), "您的支付已取消",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
        finish();
    }
}